package com.coder4j.starter.mybatis.provider;

import com.alibaba.fastjson.JSON;
import com.coder4j.starter.mybatis.SqlFieldReader;
import com.coder4j.utils.Console;
import com.coder4j.utils.StringUtils;

import java.util.List;

/**
 * 基础的Insert语句提供者
 *
 * @author Keller
 * @date 2021-02-02 07:31:08
 */
public class BaseInsertProvider {

    /**
     * 基础的添加语句
     * 读取对象的所有字段属性，生成insert语句
     * 第一次生成后，会缓存起来
     *
     * @param providerVO tableName 表名
     *                   entity 实体类
     * @param <T>
     * @return
     */
    public static <T> String insert(ProviderVO<T> providerVO) {
        T entity = providerVO.getEntity();
        String tableName = providerVO.getTableName();
        Class cls = providerVO.getEntity().getClass();
        String className = cls.getName();
        List<String> fieldList = SqlFieldReader.getFields(entity);

        StringBuilder builder = new StringBuilder();
        builder.append("INSERT INTO ")
                .append(tableName).append(" ")
                .append("(")
                .append(StringUtils.humpToLine(SqlFieldReader.getFieldStr(entity)))
                .append(") ").append("VALUES(");

        StringBuilder valuesStr = new StringBuilder();
        for (String str : fieldList) {
            valuesStr.append("#{entity.").append(str).append("}").append(",");
        }
        builder.append(valuesStr.substring(0, valuesStr.length() - 1))
                .append(") ");
        String sql = builder.toString();
        Console.info("insert", tableName, sql, JSON.toJSONString(entity));
        return sql;
    }

    /**
     * 批量添加
     *
     * @param providerVO tableName 表名
     *                   list 实体类列表
     * @param <T>
     * @return
     */
    public static <T> String insertList(ProviderVO<T> providerVO) {
        List<T> list = providerVO.getList();
        String tableName = providerVO.getTableName();
        T entity = list.get(0);
        String sql = null;
        List<String> fieldList = SqlFieldReader.getFields(entity);

        StringBuilder builder = new StringBuilder();
        builder.append("<script> ")
                .append("INSERT INTO ")
                .append(tableName).append(" ")
                .append("(")
                .append(StringUtils.humpToLine(SqlFieldReader.getFieldStr(entity)))
                .append(") ")
                .append("VALUES ")
                .append("<foreach collection='list' item='item' index='index' separator=','>");

        StringBuilder valuesStr = new StringBuilder();
        valuesStr.append("(");
        for (String str : fieldList) {
            valuesStr.append("#{item.").append(str).append("}").append(",");
        }
        builder.append(valuesStr.substring(0, valuesStr.length() - 1))
                .append(") ")
                .append("</foreach>")
                .append("</script>");
        sql = builder.toString();
        Console.info("insertList", tableName, sql, JSON.toJSONString(list));
        return sql;
    }

}
